package in.muton.edushare.poll.sections;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.questions.NewQuestion;
import in.muton.edushare.poll.util.QuestionFactory;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.edushare.wicket.models.InnerList;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.PollsDAO;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollPages;
import com.edushare.common.vo.PollQuestions;

public class PollSectionEdit extends EdusharePanel {
	private static final long serialVersionUID = -4226311323620675324L;
	private IModel<PollPages> pageModel;
	private boolean titleEdit = false;

	public PollSectionEdit(String id, IModel<PollPages> model,
			EduShareBasePage page) {
		super(id, page);
		this.pageModel = model;
		initTitleEditor();
		initQuestionList();
		initNewQuestionPanel();
	}

	private void initNewQuestionPanel() {
		add(new NewQuestion("newQuestionPanel", new CayenneModel<Poll>(
				pageModel.getObject().getPoll()), getBasePage(), pageModel) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible() {
				Poll p = pageModel.getObject().getPoll();
				if (p.getStatus() == Poll.POLL_STATUS_DRAFT
						&& p.getCreator() == EduShareSession.get()
								.getLoggedInUser())
					return true;
				return false;
			}
		});
	}

	private void initQuestionList() {
		add(new Link<Void>("deletePage") {
			private static final long serialVersionUID = -3359400102982652554L;

			@Override
			public boolean isVisible(){
				return pageModel.getObject().getPoll().getPages().size()>1;
			}
			
			@Override
			public void onClick() {
				if (pageModel.getObject().getPoll().getPages().size() > 1)
					new PollsDAO(getORMInterface()).deletePage(pageModel
							.getObject());
				else
					setError("A poll must have at least one page.");
			}
		});
		add(new ListView<IModel<PollQuestions>>("questionsList",
				new InnerList<PollQuestions>() {
					private static final long serialVersionUID = -5169030638708198244L;

					@Override
					public List<IModel<PollQuestions>> getObject() {
						List<PollQuestions> pqtList = pageModel.getObject()
								.getQuestions();
						Collections.sort(pqtList);
						CayenneList<PollQuestions> pqList = new CayenneList<PollQuestions>(
								pqtList);
						return pqList.getObject();
					}
				}) {
			private static final long serialVersionUID = 3628696159453940520L;

			@Override
			protected void populateItem(ListItem<IModel<PollQuestions>> arg0) {
				arg0.add(QuestionFactory.getQuestionPanel("question", arg0
						.getModelObject(), PollSectionEdit.this.getBasePage(),
						true, true));
			}
		});

	}

	private void initTitleEditor() {
		add(new Label("pageTitle", pageModel.getObject().getPageTitle()));
		Form<Void> f = new Form<Void>("titleEditForm");
		f.add(new TextField<String>("pageTitleBox", new Model<String>() {
			private static final long serialVersionUID = -2435099037538316227L;

			@Override
			public String getObject() {
				return pageModel.getObject().getPageTitle();
			}

			@Override
			public void setObject(String str) {
				pageModel.getObject().setPageTitle(str);
				getORMInterface().commitAllChanges();
			}
		}));
		add(f);
	}

	public static int getMaxQuestionNum(PollPages page) {
		List<PollQuestions> pq = page.getQuestions();
		int maxNum = 0;
		if (pq.size() == 0)
			return getMaxQuestionNum(page.getPoll());
		for (PollQuestions p : pq) {
			if (p.getQuestionSerialNo() > maxNum)
				maxNum = p.getQuestionSerialNo();
		}
		return maxNum;
	}

	public static int getMaxQuestionNum(Poll poll) {
		List<PollQuestions> pq = poll.getQuestions();
		int maxNum = 0;
		for (PollQuestions p : pq) {
			if (p.getQuestionSerialNo() > maxNum)
				maxNum = p.getQuestionSerialNo();
		}
		return maxNum;
	}

	public static int getMaxPageNum(Poll poll) {
		List<PollPages> pg = poll.getPages();
		int maxNum = 0;
		for (PollPages p : pg) {
			if (p.getPageNumber() > maxNum)
				maxNum = p.getPageNumber();
		}
		return maxNum;
	}

	public void setTitleEdit(boolean titleEdit) {
		this.titleEdit = titleEdit;
	}

	public boolean isTitleEdit() {
		return titleEdit;
	}

}
